package org.example;

/**
 * @author: knowno
 * @create: 2025-09-09 10:59
 * @Version 1.0
 **/
public class TestSwap {

    public static void main(String[] args) {

        int a = 10;
        int b = 20;
        swap3(a,b);
        System.out.println("main a=" + a + ",b=" + b);

    }
    /**
     * 交换a和b的值 =>临时变量
     */
    static void swap1(){
        int a = 10;
        int b = 20;
        System.out.println("a=" + a + ",b=" + b);
        //交换a和b的值 =>临时变量
        int temp = a;
        a = b;
        b = temp;
        System.out.println("a=" + a + ",b=" + b);
    }

    /**
     * 交换a和b的值 =>位运算
     */
    //交换a和b的值 =>位运算
    public static void swap2(int a,int b){
        System.out.println("swap2 a=" + a + ",b=" + b);
        a = a^b;
        b = a^b;
        a = a^b;
        System.out.println("swap2 a=" + a + ",b=" + b);
    }

    /**
     * 交换a和b的值 =>加减运算
     */
    public static void swap3(int a,int b){
        System.out.println("swap3 a=" + a + ",b=" + b);
        a = a+b;
        b = a-b;
        a = a-b;
        System.out.println("swap3 a=" + a + ",b=" + b);
    }


}
